import time
import json
import requests
import emulator_ctrl


cbWebServer = "http://192.168.31.53/variables/write/"
file = open("choose_model.cfg", "r", encoding='utf-8')
data = json.load(file)
use_dassault_model = data.get('use_dassault_model')

def doorOpen():
    if use_dassault_model=='1':
        param1 = {"BtKeyDetection": "0"}
        res = requests.get(url=cbWebServer, params=param1)
        cameraParams = {"cameraRequest": "1"}
        requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(7)

        # time.sleep(5)
        param1 = {"BtKeyDetection":"1"}
        res = requests.get(url=cbWebServer, params=param1)
        cameraParams = {"cameraRequest": "2"}
        requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(5)

        param2 = {"CabinDoorRow1LeftHandle_IsTouched":"1"}
        requests.get(url=cbWebServer, params=param2)
        time.sleep(0.5)
        param2 = {"CabinDoorRow1LeftHandle_IsTouched": "0"}
        requests.get(url=cbWebServer, params=param2)
        cameraParams = {"cameraRequest": "3"}
        requests.get(url=cbWebServer, params=cameraParams)

        param3 = {"CabinDoorRow1Left_Open":"1"}
        requests.get(url=cbWebServer, params=param3)
        time.sleep(0.5)
        param3 = {"CabinDoorRow1Left_Open": "0"}
        requests.get(url=cbWebServer, params=param3)
        time.sleep(5)
        # cameraParams = {"cameraRequest": "4"}
        # requests.get(url=cbWebServer, params=cameraParams)
        # param4 = {"carEntering":"1"}
        # requests.get(url=cbWebServer, params=param4)
        # time.sleep(0.5)
        # param4 = {"carEntering": "0"}
        # requests.get(url=cbWebServer, params=param4)
        # time.sleep(5)
    else:
        emulator_ctrl.doorOpen()

def doorClose():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest": "4"}
        requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(3)
        myParams = {"carEntering": "1"}
        requests.get(url=cbWebServer, params=myParams)
        time.sleep(0.5)
        myParams = {"carEntering": "0"}
        requests.get(url=cbWebServer, params=myParams)
        time.sleep(0.5)
        myParams = {"doorClosing": "1"}
        requests.get(url=cbWebServer, params=myParams)
        time.sleep(0.5)
        cameraParams = {"cameraRequest": "5"}
        requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(0.5)
        myParams = {"doorClosing": "0"}
        requests.get(url=cbWebServer, params=myParams)
    else:
        emulator_ctrl.doorClose()


def rearMirror():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest": "3"}
        requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(2)
        myParams = {"BtKeyDetection": "0"}
        requests.get(url=cbWebServer, params=myParams)
    else:
        pass

def seatPositionForward():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"5"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(1)
        for i in range(5):
            myParams = {"plus_PositionSeat":"1"}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(0.5)
            myParams = {"plus_PositionSeat": "0"}
            res = requests.get(url=cbWebServer, params=myParams)
    else:
        emulator_ctrl.seatPositionForward()

def seatPositionBackward():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"5"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(1)
        for i in range(5):
            myParams = {"minus_PositionSeat":"1"}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(0.5)
            myParams = {"minus_PositionSeat": "0"}
            res = requests.get(url=cbWebServer, params=myParams)
    else:
        emulator_ctrl.seatPositionBackward()

def backrestReclinePlus():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"5"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(1)
        for i in range(5):
            myParams = {"plus_BackrestRecline":"1"}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(0.5)
            myParams = {"plus_BackrestRecline": "0"}
            res = requests.get(url=cbWebServer, params=myParams)
    else:
        pass

def backrestReclineMinus():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"5"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(1)
        for i in range(5):
            myParams = {"minus_BackrestRecline":"1"}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(0.5)
            myParams = {"minus_BackrestRecline": "0"}
            res = requests.get(url=cbWebServer, params=myParams)
    else:
        pass

def seatHeightPlus():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"5"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(1)
        for i in range(5):
            myParams = {"plus_HeightSeat":"1"}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(0.5)
            myParams = {"plus_HeightSeat": "0"}
            res = requests.get(url=cbWebServer, params=myParams)
    else:
        pass

def seatHeightMinus():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"5"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(1)
        for i in range(5):
            myParams = {"minus_HeightSeat":"1"}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(0.5)
            myParams = {"minus_HeightSeat": "0"}
            res = requests.get(url=cbWebServer, params=myParams)
    else:
        pass

def setAmbientLight(color):
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"5"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(1)
        myParams = {"forceAmbiance":color}
        res = requests.get(url=cbWebServer, params=myParams)
    else:
        if color == 1:
            emulator_ctrl.setAmbientLight(color)
        else:
            emulator_ctrl.setAmbientLight(color)
            emulator_ctrl.setAmbientLight(color)

def setMarqueeMode():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"5"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        for i in range(2):
            myParams = {"forceAmbiance":1}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(1)
            myParams = {"forceAmbiance":2}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(1)
            myParams = {"forceAmbiance":3}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(1)
            myParams = {"forceAmbiance":4}
            res = requests.get(url=cbWebServer, params=myParams)
            time.sleep(1)
    else:
        emulator_ctrl.setMarqueeMode()

def controlReadingLight(isOn):
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"6"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(2)
        myParams = {"cbBpCourtesyLeft":1}
        res = requests.get(url=cbWebServer, params=myParams)
        time.sleep(0.5)
        myParams = {"cbBpCourtesyLeft":0}
        res = requests.get(url=cbWebServer, params=myParams)
        time.sleep(0.5)
        myParams = {"cbBpCourtesyRight":1}
        res = requests.get(url=cbWebServer, params=myParams)
        time.sleep(0.5)
        myParams = {"cbBpCourtesyRight":0}
        res = requests.get(url=cbWebServer, params=myParams)
    else:
        if isOn:
            emulator_ctrl.setDomeLightOn()
        else:
            emulator_ctrl.setDomeLightOff()

def controlLeftReadingLight():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"6"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(2)
        myParams = {"cbBpCourtesyLeft":1}
        res = requests.get(url=cbWebServer, params=myParams)
        time.sleep(0.5)
        myParams = {"cbBpCourtesyLeft":0}
        res = requests.get(url=cbWebServer, params=myParams)
    else:
        pass

def controlRightReadingLight():
    if use_dassault_model=='1':
        cameraParams = {"cameraRequest":"6"}
        res = requests.get(url=cbWebServer, params=cameraParams)
        time.sleep(2)
        myParams = {"cbBpCourtesyRight":1}
        res = requests.get(url=cbWebServer, params=myParams)
        time.sleep(0.5)
        myParams = {"cbBpCourtesyRight":0}
        res = requests.get(url=cbWebServer, params=myParams)
    else:
        pass

def controlFrontLight(isOn):
    if use_dassault_model=='1':
        pass
    else:
        if isOn:
            emulator_ctrl.setFrontLightOn()
        else:
            emulator_ctrl.setFrontLightOff()

def controlBrakeLight(isOn):
    if use_dassault_model=='1':
        pass
    else:
        if isOn:
            emulator_ctrl.setBrakeLightOn()
        else:
            emulator_ctrl.setBrakeLightOff()

def controlWiper(mode):
    if use_dassault_model=='1':
        pass
    else:
        emulator_ctrl.controlWiper()

def changeCameraView():
    cameraParams = {"cameraRequest": "1"}
    result = requests.get(url=cbWebServer, params=cameraParams)
    pass

if __name__ == "__main__":
    doorOpen()
    seatPositionForward()
    seatPositionBackward()
    backrestReclinePlus()
    backrestReclineMinus()
    seatHeightPlus()
    seatHeightMinus()
    setMarqueeMode()
    controlLeftReadingLight()
    controlRightReadingLight()
    doorClose()
    rearMirror()